package org.greyhawk.samonitor.domain;

import javax.persistence.Entity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.entity.RooEntity;
import org.greyhawk.samonitor.vo.AdvertStatus;
import javax.persistence.Enumerated;
import org.greyhawk.samonitor.domain.Resources;

import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Query;
import javax.persistence.Table;

import org.greyhawk.samonitor.domain.Statute;
import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;

@Entity
@RooJavaBean
@RooToString
@RooEntity
@Table(name="E_ADVERT")
public class Advert {

    @Enumerated
    private AdvertStatus advertStatus;

    @ManyToOne(targetEntity = Resources.class)
    @JoinColumn
    private Resources resources;

    @ManyToOne(targetEntity = Statute.class)
    @JoinColumn
    private Statute statute;

    private String explain;
    
    public static Query findAdvertBySiteAndStatus(ResourcesType type,Site site,AdvertStatus status){
    	String qlString = "from Advert ad where ad.resources.site = :site and ad.advertStatus = :status and ad.resources.type = :type";
    	return Advert.entityManager().createQuery(qlString).setParameter("site", site).setParameter("status", status).setParameter("type",type);
    	
    }
    
    public static Long findAdvertCountBySiteAndStatus(ResourcesType type,Site site,AdvertStatus status){
    	String qlString = "select count(*) from Advert ad where ad.resources.site = :site and ad.advertStatus = :status and ad.resources.type = :type";
    	return (Long)Advert.entityManager().createQuery(qlString).setParameter("site", site).setParameter("status", status).setParameter("type",type).getSingleResult();
    }
}
